﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using Labthink.PCSoft.UniversalHost4.Model;
using Labthink.PCSoft.UniversalHost4.UserControls;
using Labthink.PCSoft.UniversalHost4.Utility;
using Report.Model;
using DataType = Labthink.PCSoft.UniversalHost4.Model.DataType;

namespace Report.Forms
{
    /// <summary>
    /// FrmSequence.xaml 的交互逻辑
    /// </summary>
    public partial class FrmPrecision
    {
        public List<FullData> Datas { get; set; }
        public List<SelectedData> Selecteds { get; set; }
        public List<SelectedData> Seqs { get; set; }
        private const int Col = 3;
        public FrmPrecision()
        {
            InitializeComponent();
            Resources.MergedDictionaries.Add(ReportCommon.GetLan);
        }

        private void FrmSequence_OnLoaded(object sender, RoutedEventArgs e)
        {
            var rows = Math.Ceiling(Selecteds.Count / (double)Col).ToInt();
            for (int i = 0; i < rows; i++)
            {
                GridItems.RowDefinitions.Add(new RowDefinition { Height = new GridLength(50) });
            }
            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < Col; j++)
                {
                    var di = i * Col + j;
                    if (Selecteds.Count > di)
                    {
                        var sd = Datas.SingleOrDefault(d => d.DataNo == Selecteds[di].Id);

                        if (sd != null)
                        {
                            var uc = new UcTextBox
                            {
                                Label = sd.Text,
                                Margin = new Thickness(5, 5, 5, 5),
                                Tag = sd.DataNo,
                                Value = Selecteds[di].Precision.HasValue ? Selecteds[di].Precision.Value + "" : "--",
                                DataType = Labthink.PCSoft.UniversalHost4.UserControls.DataType.Int,
                                MaxValue = 10,
                                MinValue = 1
                            };
                            if (sd.DataType != DataType.Double &&
                          sd.DataType != DataType.EDouble &&
                          sd.DataType != DataType.Float) uc.IsEnabled = false;
                            //var txt = new TextBlock { Text = sd.Text, Margin = new Thickness(3, 6, 3, 0) };
                            //var input = new TextBox { Width = 60, Tag = sd.DataNo, Text = Selecteds[di].Sequence+"" };
                            //input.LostFocus += Input_LostFocus;
                            GridItems.RegisterName("txt" + sd.DataNo, uc);
                            //var sp = new StackPanel{Orientation = Orientation.Horizontal,VerticalAlignment = VerticalAlignment.Center};
                            //sp.Children.Add(txt);
                            //sp.Children.Add(input);
                            Grid.SetRow(uc, i);
                            Grid.SetColumn(uc, j);
                            GridItems.Children.Add(uc);
                        }


                    }
                }

            }
        }

        private void Input_LostFocus(object sender, RoutedEventArgs e)
        {
            var txt = sender as TextBox;
            if (txt == null) return;
            if (txt.Text == "--") return;
            var b = int.TryParse(txt.Text, out var _);
            if (!b) txt.Text = "-1";
        }

        private void BtnCancel_OnClick(object sender, RoutedEventArgs e)
        {
            Close();
        }
        private void BtnReset_OnClick(object sender, RoutedEventArgs e)
        {
            Seqs = null;
            DialogResult = true;
            Close();
        }

        private void BtnOk_OnClick(object sender, RoutedEventArgs e)
        {
            Seqs = new List<SelectedData>();
            foreach (var sel in Selecteds)
            {
                if (GridItems.FindName("txt" + sel.Id) is UcTextBox txt)
                {
                    if (txt.Value != "--" && txt.Value != "-1") Seqs.Add(new SelectedData { Id = sel.Id, Precision = txt.Value.ToInt() });
                }
            }
            DialogResult = true;
            Close();
        }

    }
}
